<template>
    <div>
        <div style="display:flex">
            <el-input style="width:200px" placeholder="查询会员账号" v-model="count"></el-input>
            <el-input style="width:200px;margin:0 5px" placeholder="查询姓名" v-model="name"></el-input>
            <el-button type="primary" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" style="background-color:rgb(182, 233, 15);border:none">重置</el-button>
        </div>
        <div style="margin:10px 0;">
            <el-button  type="info" plain @click="exportData" style="background-color:rgba(238, 238, 94, 0.556);">批量导出</el-button>
        </div>
        <el-table :data="tableData" stripe :header-cell-style="{backgroundColor:'aliceblue',color:'#666'}" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="70" align="center"></el-table-column>
            <el-table-column prop="vip" label="VIP"></el-table-column>
            <el-table-column prop="count"  label="账号"></el-table-column>
            <el-table-column prop="name" label="名字"></el-table-column>
            <el-table-column label="操作" align="center" width="180">
                <template v-slot="scope">
                    <el-button size="mini" type="danger" plain @click="handleEdit(scope.row)">取消会员</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin:10px 0;">
            <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
export default{
    name:"VIP",
    data(){
        return {
            tableData:[],
            pageNum:1,
            pageSize:5,
            count:'',
            name:'',
            total:0,
            form:{},
            user:JSON.parse(localStorage.getItem('honey-user') || '{}'),
            rules:{
                count:[
                    {required:true,message:'请输入账号',trigger:'blur'}
                ]
            },
            counts:[]
        }
    },
    created(){
        this.load()
    },
    methods:{
        handleEdit(row){
            this.form=JSON.parse(JSON.stringify(row))//给form对象赋值,注意要深拷贝
            this.$confirm('您确认取消会员吗？', '确认取消', {type: "warning"}).then(response => {
                this.form.vip="否"
                this.$request({
                    url: '/user/update',
                    method:'PUT',
                    data: this.form
                }).then(res => {
                    if (res.code === '200') {  // 表示成功保存
                        this.$message.success('保存成功')
                        this.load(1)
                        this.formVisible = false
                    } else {
                        this.$message.error(res.msg)  // 弹出错误的信息
                    }
                })
            }).catch(() => {})
        },
        exportData(){
            if(!this.counts.length){
                window.open('http://localhost:9090/user/export2?token='+this.user.token+"&count="+this.count+"&name="+this.name)
            }else{
                let countStr=this.counts.join(',')
                window.open('http://localhost:9090/user/export2?token='+this.user.token+'&counts='+countStr)
            }
        },

        handleSelectionChange(rows){
            this.counts=rows.map(v=>v.count)
        },
        
        reset(){
            this.name='',
            this.count='',
            this.load()
        },
        load(pageNum) {  // 分页查询
        if(pageNum){
            this.pageNum=pageNum
        }
        this.$request.get('/user/selectByPage2', {
        params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            count: this.count,
            name: this.name,
        }
        }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        })
        },
        handleCurrentChange(pageNum) {
            this.load(pageNum)
        },
    }
}
</script>

<style scoped>

</style>